package com.qiantu.phone.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.qiantu.phone.utils.LinkageDataHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * time   : 2021/12/22
 */
public final class SwitchStatusActivity extends AppActivity {

    public static final int TYPE_POWER = 0;
    public static final int TYPE_CURTAIN = 1;

    private TextView tvCondition;
    private RadioGroup whenRadioGroup;
    // 0关、1开
    private int mValue = 1;
    private int type;
    private String deviceSerialNo;
    private String serialNo;
    private String areaName;
    private String areaCoding;
    private String deviceName;
    private String deviceImage;
    private LinkageBean mLinkageBean;
    private LinkageBean.LinkageCondition mLinkageCondition;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_switch_status;
    }

    @Override
    protected void initView() {
        type = getInt("type");
        switch (type){
            case TYPE_POWER:
                setTitle(R.string.switch_status);
                break;
            case TYPE_CURTAIN:
                setTitle(R.string.Linkage_Device_CurtainState);
                break;
        }
        tvCondition = findViewById(R.id.tv_condition);
        whenRadioGroup = findViewById(R.id.when_radio_group);
        whenRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.type_on:
                        switch (type){
                            case TYPE_POWER:
                                mValue = 1;
                                break;
                            case TYPE_CURTAIN:
                                mValue = 2;
                                break;
                        }
                        tvCondition.setText(getString(R.string.effective_when_time_holder, getString(R.string.switch_on)));
                        break;
                    case R.id.type_off:
                        switch (type){
                            case TYPE_POWER:
                                mValue = 0;
                                break;
                            case TYPE_CURTAIN:
                                mValue = 1;
                                break;
                        }
                        tvCondition.setText(getString(R.string.effective_when_time_holder, getString(R.string.switch_off)));
                        break;
                }
            }
        });
        ((RadioButton)whenRadioGroup.getChildAt(0)).setChecked(true);
    }

    @Override
    protected void initData() {
        serialNo = getString("serial_no");
        deviceSerialNo = getString("device_serial_no");
        areaCoding = getString("room_serial_no");
        areaName = getString("floor_room_name");
        deviceName = getString("device_name");
        deviceImage = getString("device_image");
        mLinkageBean = LinkageDataHolder.getLinkageData();
        if (mLinkageBean != null) {
            List<LinkageBean.LinkageCondition> linkageConditions = mLinkageBean.getLinkageConditions();
            if (linkageConditions != null && linkageConditions.size() > 0 && !TextUtils.isEmpty(serialNo)) {
                for (LinkageBean.LinkageCondition item : linkageConditions) {
                    if (item.getConditionSerialNo().equals(serialNo)) {
                        mLinkageCondition = item;
                        deviceSerialNo = mLinkageCondition.getDeviceSerialNo();
                        deviceName = LLDeviceDBManager.getInstance(SwitchStatusActivity.this).get(deviceSerialNo).getName();
                        areaName = mLinkageCondition.getAreaName();
                        areaCoding = mLinkageCondition.getAreaCoding();
                        try {
                            mValue = Integer.valueOf(mLinkageCondition.getStateValue());
                        }catch (Exception e){

                        }
                        break;
                    }
                }
            }
        }
        updateView();
    }

    private void updateView() {
        switch (type){
            case TYPE_POWER:
                switch (mValue) {
                    case 1:
                        ((RadioButton) whenRadioGroup.getChildAt(0)).setChecked(true);
                        break;
                    case 0:
                        ((RadioButton) whenRadioGroup.getChildAt(2)).setChecked(true);
                        break;
                }
                break;
            case TYPE_CURTAIN:
                switch (mValue) {
                    case 2:
                        ((RadioButton) whenRadioGroup.getChildAt(0)).setChecked(true);
                        break;
                    case 1:
                        ((RadioButton) whenRadioGroup.getChildAt(2)).setChecked(true);
                        break;
                }
                break;
        }

    }

    @Override
    public void onRightClick(View view) {
        //当前联动空，才去
        if (mLinkageCondition == null) {
            //本地保存联动条件
            if (mLinkageBean == null) {
                mLinkageBean = new LinkageBean();
                LinkageDataHolder.setLinkageData(mLinkageBean);
            }
            List<LinkageBean.LinkageCondition> linkageConditions = mLinkageBean.getLinkageConditions();
            if (linkageConditions == null) {
                linkageConditions = new ArrayList<>();
            }
            mLinkageBean.setLinkageConditions(linkageConditions);
            mLinkageCondition = new LinkageBean.LinkageCondition();
            //先设置个本地的SerialNo，为了编辑找到对应的数据
            mLinkageCondition.setConditionSerialNo(UUID.randomUUID().toString());
            mLinkageCondition.setConditionType(8);
            linkageConditions.add(mLinkageCondition);
            mLinkageCondition.setSort(linkageConditions.indexOf(mLinkageCondition) + 1);
        }
        mLinkageCondition.setAreaType(1);
        mLinkageCondition.setDeviceSerialNo(deviceSerialNo);
        mLinkageCondition.setAreaName(areaName);
        mLinkageCondition.setAreaCoding(areaCoding);
        mLinkageCondition.setBeginValue(mValue);
        mLinkageCondition.setEndValue(mValue);
        mLinkageCondition.setLogicType(2);
        mLinkageCondition.setImageUrl(deviceImage);
        mLinkageCondition.setBriefing(deviceName);
        switch (type){
            case TYPE_POWER:
                mLinkageCondition.setStateName(LLDeviceUtil.POWER);
                break;
            case TYPE_CURTAIN:
                mLinkageCondition.setStateName(LLDeviceUtil.STROKE_STATE);
                break;
        }
        mLinkageCondition.setStateValue(mValue + "");
        startActivity_SingleTask(EditLinkageActivity.class);
        finish();
    }
}